package com.njtoyo.taxi.admin.logic.business.driver;

import cn.hutool.core.collection.CollUtil;
import com.njtoyo.taxi.admin.library.taxi_service.TaxiService;
import com.njtoyo.taxi.admin.mapper.master.entity.DriverNotifyMessage;
import com.njtoyo.taxi.admin.mapper.master.entity.DriverNotifyMessageRead;
import com.njtoyo.taxi.admin.mapper.master.enums.driverNotifyMessage.PriorityCategory;
import com.njtoyo.taxi.admin.mapper.master.enums.driverNotifyMessage.State;
import com.njtoyo.taxi.admin.mapper.master.mapper.DriverMapper;
import com.njtoyo.taxi.admin.mapper.master.mapper.DriverNotifyMessageMapper;
import com.njtoyo.taxi.admin.mapper.master.mapper.DriverNotifyMessageReadMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Objects;

@Service
@Slf4j
public class DriverNotifyMessageLogic {

    @Autowired
    private DriverNotifyMessageMapper driverNotifyMessageMapper;

    @Autowired
    private DriverMapper driverMapper;

    @Autowired
    private DriverNotifyMessageReadMapper driverNotifyMessageReadMapper;

    @Autowired
    private TaxiService taxiService;

    @Transactional()
    public void send(Long messageId) {
        log.info("正在准备发送司机通知消息，消息id: {}", messageId);
        DriverNotifyMessage message = driverNotifyMessageMapper.selectById(messageId);
        if (Objects.isNull(message)) {
            log.info("司机通知消息ID不存在");
            return;
        }
        log.info("司机通知消息: {}", message);
        if (message.getState().equals(State.done)) {
            log.info("司机通知消息已被标记完成");
            return;
        }
        Long nowTime = (new Date()).getTime() / 1000;
        Long startTime = nowTime - 40;
        Long endTime = nowTime + 40;
        if (message.getRegularReleaseTime() < startTime || message.getRegularReleaseTime() > endTime) {
            log.info("司机通知消息发布时间不满足要求");
            return;
        }

        log.info("正在操作数据库");
        insertBatchDriverNotifyMessageRead(message);

        log.info("正在准备发送外部通知");
        taxiService.pushDriverNotifyMessage(messageId);
    }

    boolean insertBatchDriverNotifyMessageRead(DriverNotifyMessage message) {
        List<Long> driverIds = new ArrayList<>();
        switch (message.getScope()) {
            case all:
                driverIds = driverMapper.findDriverIdsAll();
                break;
            case region:
                driverIds = driverMapper.findDriverIdsByDriverRegionId(message.getDriverRegionId());
                break;
            case company:
                driverIds = driverMapper.findDriverIdsByCompanyId(message.getCompanyId());
                break;
        }
        if (CollUtil.isNotEmpty(driverIds)) {
            List<DriverNotifyMessageRead> messageRead = new ArrayList<>();
            driverIds.forEach(driverId -> {
                DriverNotifyMessageRead read = new DriverNotifyMessageRead();
                read.setMessageId(message.getId());
                read.setDriverId(driverId);
                read.setIsRead(message.getPriorityCategory().equals(PriorityCategory.common));
                messageRead.add(read);
            });
            driverNotifyMessageReadMapper.insertBatch(messageRead);
        } else {
            log.info("该条件下不存在司机");
        }

        DriverNotifyMessage newMessage = new DriverNotifyMessage();
        newMessage.setId(message.getId());
        newMessage.setState(State.done);
        newMessage.setDriverRegionId(message.getDriverRegionId());
        newMessage.setCompanyId(message.getCompanyId());
        driverNotifyMessageMapper.updateById(newMessage);

        return true;
    }
}
